﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for algorithmPassword
/// </summary>
public class algorithmPassword
{
    
    public static string Encode(string strText, string KeySecure)
    {
        String res = String.Empty;
        string strKeySecure = KeySecure;
        if (string.IsNullOrEmpty(strText))
            return res.ToString();

        int nIndexKey = 0;
        int nLenKey = strKeySecure.Length;
        int nLen = strText.Length;
        int nIdx = 0;
        for (; nIdx < nLen; nIdx++)
        {
            nIndexKey = nIdx % nLenKey;
            if (nIndexKey >= nLenKey)
                nIndexKey %= nLenKey;
            res += Convert.ToString((char)(strText[nIdx] ^ strKeySecure[nLenKey - nIndexKey - 1]));
        }
        byte[] data = System.Text.Encoding.UTF8.GetBytes(res.ToString());
        return Convert.ToBase64String(data);
    }

    public static string Decode(string strText, string KeySecure)
    {
        String res = String.Empty;
        string strKeySecure = KeySecure;
        if (string.IsNullOrEmpty(strText))
            return res.ToString();

        strText = Encoding.UTF8.GetString(Convert.FromBase64String(strText));
        int nIndexKey = 0;
        int nLenKey = strKeySecure.Length;
        int nLen = strText.Length;
        int nIdx = 0;
        for (; nIdx < nLen; nIdx++)
        {
            nIndexKey = nIdx % nLenKey;
            if (nIndexKey >= nLenKey)
                nIndexKey %= nLenKey;
            res += Convert.ToString((char)(strText[nIdx] ^ strKeySecure[nLenKey - nIndexKey - 1]));
        }
        return res.ToString();
    }

}